package com.bw.zg3park2212.adpter


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback

import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bw.base.entity.ParkTypeEntityItem
import com.bw.zg3park2212.R
import com.bw.zg3park2212.databinding.ItemCwglBinding


class ParkTyprAdpter:PagingDataAdapter<ParkTypeEntityItem,ParkTyprAdpter.ParkViewHolder>(diff) {

    class ParkViewHolder(itemview:View):ViewHolder(itemview){
        val binding=DataBindingUtil.bind<ItemCwglBinding>(itemview)
    }
    companion object{
        val diff=object : ItemCallback<ParkTypeEntityItem>(){
            override fun areItemsTheSame(
                oldItem: ParkTypeEntityItem,
                newItem: ParkTypeEntityItem,
            ): Boolean {
                return oldItem==newItem
            }

            override fun areContentsTheSame(
                oldItem: ParkTypeEntityItem,
                newItem: ParkTypeEntityItem,
            ): Boolean {
                return oldItem.spaceStatus==newItem.spaceStatus
            }

        }
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        val item=getItem(position)!!
        holder.binding?.apply {
           cwglSpaceNumber.setText(item.spaceNumber)
            cwglSpaceStatus.setText(item.spaceStatus)
            cwglAreaName.setText("所属区域:"+item.areaName)
            cwglSpaceType.setText("车位类型:"+item.spaceType)
            cwglUserName.setText("使用人:"+item.userName)
            cwglLicensePlate.setText("车牌号:"+item.licensePlate)
            if (item.spaceStatus=="空闲"){
                cwglSpaceStatus.setTextColor(holder.itemView.context.getColor(R.color.color_kongxian))
                cwglSpaceStatus.setBackgroundResource(R.drawable.bg_kongxian)
            }else if (item.spaceStatus=="维修中"){
                cwglSpaceStatus.setTextColor(holder.itemView.context.getColor(R.color.color_weihu))
                cwglSpaceStatus.setBackgroundResource(R.drawable.bg_weihu)
            }else{
                cwglSpaceStatus.setTextColor(holder.itemView.context.getColor(R.color.color_zhanyong))
                cwglSpaceStatus.setBackgroundResource(R.drawable.bg_zganyong)
            }
        }

        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(position, item)
        }
        holder.itemView.setOnLongClickListener {
            mOnItemClickLongListener?.onItemLongClick(position, item)
            return@setOnLongClickListener true
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_cwgl, parent, false)
        return ParkViewHolder(view)
    }
    //点击事件
    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemClickLongListener: OnItemClickLongListener? = null
    interface OnItemClickListener{
        fun onItemClick(position: Int,item:ParkTypeEntityItem)
    }
    interface OnItemClickLongListener{
        fun onItemLongClick(position: Int,item:ParkTypeEntityItem)
    }
    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mOnItemClickListener=listener
    }
    fun setOnItemClickLongListener(listener: OnItemClickLongListener) {
        this.mOnItemClickLongListener=listener
    }
}